package com.ruoyi.accounting.enums;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 流水类型枚举
 */
public enum AccountingRecordTypeEnum {
    INCOME(1, "收入", ClassificationTypeEnum.INCOME),
    EXPENDITURE(2, "支出", ClassificationTypeEnum.EXPENDITURE),
    TRANSFER(3, "转账", null),
    REFUND(4, "退款", null);
    private final Integer code;
    private final String name;
    private final ClassificationTypeEnum classificationTypeEnum;

    AccountingRecordTypeEnum(Integer code, String name, ClassificationTypeEnum classificationTypeEnum) {
        this.code = code;
        this.name = name;
        this.classificationTypeEnum = classificationTypeEnum;
    }

    public static boolean isValidCode(Integer code) {
        for (AccountingRecordTypeEnum recordType : AccountingRecordTypeEnum.values()) {
            if (Objects.equals(recordType.getCode(), code)) {
                return true;
            }
        }

        return false;
    }

    public Integer getCode() {
        return code;
    }

    public String getName() {
        return name;
    }

    public static ClassificationTypeEnum getClassificationTypeEnum(Integer code) {
        for (AccountingRecordTypeEnum recordType : AccountingRecordTypeEnum.values()) {
            if (Objects.equals(recordType.getCode(), code)) {
                return recordType.classificationTypeEnum;
            }
        }

        return null;
    }
    public static AccountingRecordTypeEnum getAccountingRecordTypeEnum(String name) {
        for (AccountingRecordTypeEnum recordType : AccountingRecordTypeEnum.values()) {
            if (StringUtils.equals(recordType.getName(), name)) {
                return recordType;
            }
        }

        return null;
    }
    public static List<String> getNames() {
        return Arrays.stream(AccountingRecordTypeEnum.values())
                .map(AccountingRecordTypeEnum::getName)
                .collect(Collectors.toList());
    }
}
